package com.school.adminServlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.school.adminServlet.cmd.AdminCommand;
import com.school.adminServlet.cmd.AdminHomeCommand;
import com.school.adminServlet.cmd.AttendanceManagerCommand;
import com.school.adminServlet.cmd.AdminProfileCommand;
import com.school.adminServlet.cmd.CreateNonteacherProfileFormCommand;
import com.school.adminServlet.cmd.CreateStudentProfileFormCommand;
import com.school.adminServlet.cmd.CreateTeacherProfileFormCommand;
import com.school.adminServlet.cmd.DeleteNewsFormCommand;
import com.school.adminServlet.cmd.DeleteNewsFormCommand2;
import com.school.adminServlet.cmd.EmployeeAttendanceCommand;
import com.school.adminServlet.cmd.ExamManageCommand;
import com.school.adminServlet.cmd.GetStusentListCommand;
import com.school.adminServlet.cmd.InsertNewsFormCommand;
import com.school.adminServlet.cmd.InsertNewsFormCommand2;
import com.school.adminServlet.cmd.ManageNewsCommand;
import com.school.adminServlet.cmd.ManageNonteacherProfileFormCommand;
import com.school.adminServlet.cmd.ManageStudentProfileFormCommand;
import com.school.adminServlet.cmd.ManageTeacherProfileFormCommand;
import com.school.adminServlet.cmd.ProfileManageCommand;
import com.school.adminServlet.cmd.ScheduleDateFormCommand;
import com.school.adminServlet.cmd.SetExamStatusForm1Command;
import com.school.adminServlet.cmd.SetResultForm1Command;
import com.school.adminServlet.cmd.StuAttMangerCommannd;
import com.school.adminServlet.cmd.StuAttReportCommand;
import com.school.adminServlet.cmd.StudentAttendanceCommannd;
import com.school.adminServlet.cmd.StudentDetailsCommand;
import com.school.adminServlet.cmd.TeaAttMangerCommand;
import com.school.adminServlet.cmd.TeaAttReportCommand;
import com.school.adminServlet.cmd.TimeTableCommand;
import com.school.adminServlet.cmd.UpdateNewsFormCommand;
import com.school.adminServlet.cmd.UpdateNewsFormCommand2;
import com.school.adminServlet.cmd.ViewAllRegisteredCommand;
import com.school.adminServlet.cmd.GradeManagerCommand;



/**
 * This servlet will act as admin controller which will accept request from and send response to admin only.
 * @author KK
 */

	public class AdminController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static Logger log = Logger.getLogger(AdminController.class.getSimpleName());
	
	
	private ServletConfig config;
	
	private Map<String, AdminCommand> adminCommands;
	
	
	public void init(ServletConfig config) {
		log.info("Entered init(ServletConfig)");
		this.config = config;
		log.info("Cached the ServletConfig object");
		
		adminCommands = new HashMap<String, AdminCommand>();
		adminCommands.put("adminProfile", new AdminProfileCommand());
		adminCommands.put("adminHome", new AdminHomeCommand());
		adminCommands.put("viewAllRegistered", new ViewAllRegisteredCommand());
		adminCommands.put("scheduleDateForm", new ScheduleDateFormCommand());
		adminCommands.put("setResultForm1", new SetResultForm1Command());		//After submitting (set_result) form.
	
		adminCommands.put("setExamStatusForm1", new SetExamStatusForm1Command());	//For displaying form only
		adminCommands.put("deleteNewsForm", new DeleteNewsFormCommand());
		adminCommands.put("updateNewsForm", new UpdateNewsFormCommand());
		adminCommands.put("insertNewsForm", new InsertNewsFormCommand());
		adminCommands.put("insertNewsForm2", new InsertNewsFormCommand2());
		adminCommands.put("deleteNewsForm2", new DeleteNewsFormCommand2());
		adminCommands.put("updateNewsForm2", new UpdateNewsFormCommand2());
		
		adminCommands.put("profileManage", new ProfileManageCommand());
		adminCommands.put("createStudentProfileForm", new CreateStudentProfileFormCommand());
		adminCommands.put("createTeacherProfileForm", new CreateTeacherProfileFormCommand());
		adminCommands.put("createNonteacherProfileForm", new CreateNonteacherProfileFormCommand());
		adminCommands.put("manageStudentProfileForm", new ManageStudentProfileFormCommand());
		adminCommands.put("manageTeacherProfileForm", new ManageTeacherProfileFormCommand());
		adminCommands.put("manageNonteacherProfileForm", new ManageNonteacherProfileFormCommand());
		
		adminCommands.put("examManage", new ExamManageCommand());
		adminCommands.put("timeTable", new TimeTableCommand());
		adminCommands.put("attendanceManager", new AttendanceManagerCommand());
		adminCommands.put("studentDetails", new StudentDetailsCommand());
		adminCommands.put("manageNews", new ManageNewsCommand());
		
		adminCommands.put("studentAttendance", new StudentAttendanceCommannd());
		adminCommands.put("employeeAttendance", new EmployeeAttendanceCommand());
		adminCommands.put("stuAttM", new StuAttMangerCommannd());
		adminCommands.put("stuAttR", new StuAttReportCommand());
		adminCommands.put("teaAttM", new TeaAttMangerCommand());
		adminCommands.put("teaAttR", new TeaAttReportCommand());
		adminCommands.put("getStudentList", new GetStusentListCommand());
		//
		adminCommands.put("gradeManager", new GradeManagerCommand());
		
		config.getServletContext().setAttribute("adminCommands", adminCommands);
		
		log.info("Exited init(ServletConfig)");
	}
	
	public void init() {
		log.info("Entered init()");
		log.info("Exited init()");
	}
    
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		log.info("Entered doGet()");
		processRequest(req, resp);
		log.info("Exited doGet()");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		log.info("Entered doPost()");
		processRequest(req, resp);
		log.info("Exited doPost()");
	}
	
	
	private void processRequest(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
		log.info("Entered processRequest()");
		HttpSession session = req.getSession();		/////////
		String adminCommand = req.getParameter("cmd");
//		log.info("Command received is " + adminCommand);
		System.out.println("Command received is " + adminCommand);
		
		if (adminCommands.get(adminCommand) == null) {
			adminCommand = "adminHome";
		}
		
		if(session.getAttribute("type")== "admin"){
		adminCommands.get(adminCommand).processRequest(req, resp, config);
		}
		else {
			System.out.println("Acting very smart?? trying to hack?? Go to hell!!!");
		}
		
		log.info("Exited processRequest()");
	}

}
